package whatHandlers;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WebParserRegex {
	/*
	 * Parses strings from what.cd with regex's to narrow down to specified
	 * target.
	 */

	private Pattern PATTERN;
	private Matcher MATCHER;
	
	// FindDownloadString MUST be used before any other regex's
	private final String FindDownloadString = "<a\\ href=\"torrents\\.php\\?action=download&amp;.+\"\\ title=\"Download\">";

	private final String IDRegex = "id.[0-9]+\b";
	
	//Excluding these two.
	
	private final String RawUserIDRegex = "\"user\\.php\\?id=162790\" class=\"username\"";
	private final String UserIDRegex = "user\\.php\\?id=[0-9]+";

	public List<String> ParseDownloadLinks(String Page) {
		List<String> DownloadLinks = null;
		PATTERN = Pattern.compile(FindDownloadString);
		MATCHER = PATTERN.matcher(Page);
		boolean matchFound = MATCHER.find();

		do {

			DownloadLinks.add(MATCHER.group());
			System.out.println(MATCHER.group());
			matchFound = MATCHER.find();

		} while (matchFound);

		return DownloadLinks;
	}
	
	public String getUserIDLink(String RawPage){
		String Tmp = new String();
		PATTERN = Pattern.compile(RawUserIDRegex);
		MATCHER = PATTERN.matcher(RawPage);
		MATCHER.find();
		Tmp = MATCHER.group();
		PATTERN = Pattern.compile(UserIDRegex);
		MATCHER = PATTERN.matcher(Tmp);
		MATCHER.find();
		Tmp = MATCHER.group();
		return Tmp;
	}

}
